/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddAccountForm.java
 *
 * Created on Mar 18, 2011, 6:34:46 AM
 */

package GUI;

import Entities.Administrator;
import Business.AdminManage;
import Business.Encrypt;
import java.sql.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class AddAccountForm extends javax.swing.JDialog {

    /** Creates new form AddAccountForm */
    public AddAccountForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
     private void SetText()
    {
       txtAdminName.setText("");
       txtUserName.setText("");
       txtPassWord.setText("");
       txtEmail.setText("");
    }
 private boolean isValidEmailAddress(String emailAddress){
       String  expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
       CharSequence inputStr = emailAddress;
       Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
       Matcher matcher = pattern.matcher(inputStr);
       return matcher.matches();
    }
//    private boolean isValidNumber(String number){
//       String  expression="[0-9]{6,20}";
//       CharSequence inputStr = number;
//       Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
//       Matcher matcher = pattern.matcher(inputStr);
//       return matcher.matches();
//    }

  private void FormProcessing()
 {
         //if(txtBoothID.getText()>9 || txtBoothID.getText()<0)
        if(txtAdminName.getText().length()==0)
        {
            JOptionPane.showMessageDialog(rootPane, "AdminName field is not blank !");
            txtAdminName.requestFocusInWindow();
            return;
        }
       if(txtUserName.getText().length()==0)
        {
            JOptionPane.showMessageDialog(rootPane, "UserNanme field is not blank !");
            txtUserName.requestFocusInWindow();
            return;
        }
       if(txtPassWord.getText().length()==0)
        {
            JOptionPane.showMessageDialog(rootPane, "PassWord field is not blank !");
            txtPassWord.requestFocusInWindow();
            return;
        
        }
        if(txtEmail.getText().length()==0)
        {
            JOptionPane.showMessageDialog(rootPane, "Email field is not blank !");
            txtEmail.requestFocusInWindow();
            return;
        }
        if(isValidEmailAddress(txtEmail.getText()))
        {
            JOptionPane.showMessageDialog(rootPane, "Email field is invalid !");
            txtEmail.requestFocusInWindow();
            return;
        }
       

 }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bntReset = new javax.swing.JButton();
        bntCancel = new javax.swing.JButton();
        bntAdd = new javax.swing.JButton();
        txtUserName = new javax.swing.JTextField();
        txtAdminName = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        cbUserType = new javax.swing.JComboBox();
        txtPassWord = new javax.swing.JPasswordField();
        cbxStatus = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        bntReset.setText("Reset");
        bntReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntResetActionPerformed(evt);
            }
        });

        bntCancel.setText("Cancel");
        bntCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelActionPerformed(evt);
            }
        });

        bntAdd.setText("ADD");
        bntAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAddActionPerformed(evt);
            }
        });

        jLabel9.setText("Status");

        jLabel2.setText("Admin Name");

        jLabel3.setText("User Name");

        jLabel4.setText("PassWord");

        jLabel5.setText("UserType");

        jLabel6.setText("Email");

        txtEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEmailActionPerformed(evt);
            }
        });

        cbUserType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Admin", "Administrator" }));

        cbxStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Normal", "Block" }));
        cbxStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxStatusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(73, 73, 73)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel6))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addComponent(bntAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, Short.MAX_VALUE)
                            .addComponent(jLabel9)
                            .addGap(31, 31, 31)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtEmail, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtUserName, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtAdminName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                        .addComponent(txtPassWord, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(cbxStatus, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cbUserType, javax.swing.GroupLayout.Alignment.LEADING, 0, 105, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(bntReset)
                        .addGap(28, 28, 28)
                        .addComponent(bntCancel)))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtAdminName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtPassWord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cbUserType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cbxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntAdd)
                    .addComponent(bntReset)
                    .addComponent(bntCancel))
                .addGap(42, 42, 42))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bntResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntResetActionPerformed
        // TODO add your handling code here:
        SetText();
}//GEN-LAST:event_bntResetActionPerformed

    private void bntCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_bntCancelActionPerformed

    private void bntAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntAddActionPerformed
        // TODO add your handling code here:
        FormProcessing();
        String pass=txtPassWord.getText();
        Encrypt encrypt=new Encrypt();
        Administrator objAdmin=new Administrator();
        AdminManage objModel=new AdminManage();
        objAdmin.setName(txtAdminName.getText());
        objAdmin.setUserName(txtUserName.getText());
        objAdmin.setPassWord(pass);//encrypt.encryptMD5(pass));//call encryptMD5() method to encrypt password
        objAdmin.setEmail(txtEmail.getText());
        int index=cbUserType.getSelectedIndex();
        if(index > -1)
        {
           objAdmin.setUserType((String) cbUserType.getSelectedItem());
        }
        if(cbxStatus.getSelectedIndex()>-1){
           if(cbxStatus.getSelectedItem().equals("Normal")) 
             objAdmin.setStatus(1);
           else
               if(cbxStatus.getSelectedItem().equals("Block"))
                    objAdmin.setStatus(0);
        }
        Date todayD=new Date(System.currentTimeMillis());
        objAdmin.setRegisterDate(todayD);
        objAdmin.setLastvisitDate(todayD);
        objAdmin.setModifiedDate(todayD);
        if(objModel.AddAdmin(objAdmin))
        {
         JOptionPane.showMessageDialog(rootPane, "success !");
        }

}//GEN-LAST:event_bntAddActionPerformed

    private void txtEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEmailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtEmailActionPerformed

    private void cbxStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxStatusActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbxStatusActionPerformed

    /**
    * @param args the command line arguments
    *//*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddAccountForm dialog = new AddAccountForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntAdd;
    private javax.swing.JButton bntCancel;
    private javax.swing.JButton bntReset;
    private javax.swing.JComboBox cbUserType;
    private javax.swing.JComboBox cbxStatus;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtAdminName;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JPasswordField txtPassWord;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables

}
